package com.yhy.manager.listener;

import com.yhy.autoconfig.templates.HuaWeiUGCTemplate;
import com.yhy.domain.mongo.Publish;
import com.yhy.dubbo.api.mongo.PublishApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        topic = "tanhua-publishes",
        consumerGroup = "tanhua-publish-consumer"
)
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        System.out.println("开始处理"+publishId+"的动态");
//        1.通过api查出publish
        Publish publish = publishApi.findById(publishId);
//        2.如果消息为未审核
        if(publish.getState()==0){
            boolean textCheck = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            boolean imageCheck = huaWeiUGCTemplate.imageContentCheck(publish.getMedias().toArray(new String[]{}));
            Integer state = 2;
            if(textCheck && imageCheck){
                state =1;
            }
            publishApi.updateState(publishId,state);
        }
    }
}
